PROTOTYPE ACTIVE
Motion Detection Alarm System
Smart detection with real-time alert prototype — PIR + ESP32 + Buzzer
Project Overview
PIR Sensor
Detects infrared heat changes (motion) in environment.
ESP32 Processor
Reads sensor & triggers output logic.
Buzzer Alert
Generates audible alarm when motion detected.
Interactive Simulation
Wokwi Virtual Prototype
Experience the circuit in real time. See how PIR triggers the buzzer through ESP32.
Launch SimulationOpens in Wokwi (new tab)
Source Code (Arduino)
motion_detector.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#define PIR_PIN 14
#define BUZZER_PIN 27
void setup() {
pinMode(PIR_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
int motion = digitalRead(PIR_PIN);
if (motion == HIGH) {
Serial.println("Motion Detected!");
digitalWrite(BUZZER_PIN, HIGH);
delay(300);
digitalWrite(BUZZER_PIN, LOW);
delay(300);
} else {
Serial.println("No Motion");
digitalWrite(BUZZER_PIN, LOW);
}
}
System Flow
PIR Sensor
ESP32
Buzzer
Motion detected → ESP32 processes → Buzzer alarm pattern
Key Features
Motion Detection
Real-time PIR sensing
Buzzer Alert
Audible warning pattern
Local Monitoring
Serial output live
Future Upgrades Coming Soon
WiFi Integration
Remote alerts via network
Telegram Notify
Instant message alerts
Final Code
final_motion_wifi_telegram.ino
#include <WiFi.h>
#include <HTTPClient.h>
#include <stdio.h>
#include <string.h>
const char* ssid = "dipro";
const char* password = "133082133082";
const char* botToken = "8306443577:AAF6Wu5ShWFWbzu7x1NsWUhBg173-m4EFnM";
const char* chatID = "5633702420";
#define PIR_PIN 2
#define BUZZER_PIN 4
typedef struct {
int state;
int motionCount;
int systemEnabled;
unsigned long stateStartTime;
unsigned long lastMsgTime;
} SystemData;
SystemData sys;
long lastUpdateID = 0;
void connectWiFi();
void sendTelegram(char *msg, int force);
void getUpdates(char *buffer);
void processCommand(char *msg, SystemData *s);
void handleMotion(SystemData *s);
void initSystem(SystemData *s);
void connectWiFi() {
WiFi.begin(ssid, password);
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi Connected!");
}
void sendTelegram(char *msg, int force) {
if (!force && millis() - sys.lastMsgTime < 3000) return;
HTTPClient http;
char encoded[200];
int i, j = 0;
for (i = 0; msg[i] != '\0'; i++) {
if (msg[i] == ' ') {
encoded[j++] = '%';
encoded[j++] = '2';
encoded[j++] = '0';
} else {
encoded[j++] = msg[i];
}
}
encoded[j] = '\0';
char url[400];
sprintf(url,
"https://api.telegram.org/bot%s/sendMessage?chat_id=%s&text=%s",
botToken, chatID, encoded);
http.begin(url);
int code = http.GET();
Serial.print("Telegram HTTP Code: ");
Serial.println(code);
http.end();
sys.lastMsgTime = millis();
}
void getUpdates(char *buffer) {
HTTPClient http;
char url[200];
sprintf(url,
"https://api.telegram.org/bot%s/getUpdates?offset=%ld",
botToken, lastUpdateID + 1);
http.begin(url);
int code = http.GET();
if (code > 0) {
String payload = http.getString();
payload.toCharArray(buffer, 1000);
char *idPtr = strstr(buffer, "\"update_id\":");
if (idPtr != NULL) {
long id = atol(idPtr + 12);
lastUpdateID = id;
}
}
http.end();
}
void processCommand(char *msg, SystemData *s) {
if (strstr(msg, "/on")) {
s->systemEnabled = 1;
s->state = 0;
sendTelegram("System ON", 1);
}
else if (strstr(msg, "/off")) {
s->systemEnabled = 0;
digitalWrite(BUZZER_PIN, LOW);
s->state = 0;
sendTelegram("System OFF", 1);
}
else if (strstr(msg, "/status")) {
char info[100];
sprintf(info, "System:%s Count:%d",
s->systemEnabled ? "ON" : "OFF",
s->motionCount);
sendTelegram(info, 1);
}
else if (strstr(msg, "/reset")) {
s->motionCount = 0;
sendTelegram("Counter Reset!", 1);
}
}
void handleMotion(SystemData *s) {
int motion = digitalRead(PIR_PIN);
unsigned long now = millis();
switch (s->state) {
// IDLE
case 0:
if (motion == HIGH) {
s->motionCount++;
digitalWrite(BUZZER_PIN, HIGH);
sendTelegram("Motion Detected!", 1);
Serial.println("STATE: ALARM");
s->state = 1;
s->stateStartTime = now;
}
break;
case 1:
if (now - s->stateStartTime >= 25000) {
digitalWrite(BUZZER_PIN, LOW);
sendTelegram("Cooldown Start", 0);
Serial.println("STATE: COOLDOWN");
s->state = 2;
s->stateStartTime = now;
}
break;
case 2:
if (now - s->stateStartTime >= 3000) {
Serial.println("STATE: IDLE");
s->state = 0;
}
break;
}
}
void initSystem(SystemData *s) {
s->state = 0;
s->motionCount = 0;
s->systemEnabled = 1;
s->stateStartTime = 0;
s->lastMsgTime = 0;
}
void setup() {
Serial.begin(115200);
pinMode(PIR_PIN, INPUT_PULLDOWN);
pinMode(BUZZER_PIN, OUTPUT);
initSystem(&sys);
connectWiFi();
Serial.println("PIR Warming up...");
delay(30000);
Serial.println("Ready!");
sendTelegram("System Online", 1);
}
void loop() {
char response[1000];
getUpdates(response);
processCommand(response, &sys);
if (sys.systemEnabled == 1) {
handleMotion(&sys);
} else {
digitalWrite(BUZZER_PIN, LOW);
}
delay(200);
}
motion_sim.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
//credentials
#define WIFI_NAME "dipro"
#define WIFI_PASSWORD "133082133082"
#define BOT_TOKEN "8306443577:AAF6Wu5ShWFWbzu7x1NsWUhBg173-m4EFnM"
#define CHAT_ID "5633702420"
//pin define
#define PIR_PIN 4
#define BUZZER_PIN 2
#define HIGH 1
#define LOW 0
int motionState = 0;
int systemEnabled = 1;
int motionCount = 0;
long lastUpdateID = 0;
int buzzerState = 0;
int pirSensorVal = 0;
void connectWiFi();
void sendTelegram(const char* msg);
int readMotion();
void handleMotion(int motion);
void turnBuzzerOn();
void turnBuzzerOff();
void checkCommands(char* input);
void processCommand(const char* msg);
int digitalRead(int pin);
void digitalWrite(int pin, int value);
void pinMode(int pin, int mode);
void printSeparator();
void printStatus();
void pinMode(int pin, int mode) {
printf("[PIN] GPIO %d configured\n", pin);
}
int digitalRead(int pin) {
if (pin == PIR_PIN) return pirSensorVal;
return 0;
}
void digitalWrite(int pin, int value) {
if (pin == BUZZER_PIN) {
buzzerState = value;
}
}
void connectWiFi() {
printf("[WiFi] Connecting to '%s'", WIFI_NAME);
fflush(stdout);
int i;
for (i = 0; i < 5; i++) {
printf(".");
fflush(stdout);
}
printf(" Connected!\n");
}
void sendTelegram(const char* msg) {
printf("\n[TELEGRAM] >>> %s\n", msg);
}
int readMotion() {
return digitalRead(PIR_PIN);
}
void turnBuzzerOn() {
digitalWrite(BUZZER_PIN, HIGH);
printf("[BUZZER] ON - BEEP BEEP!\n");
}
void turnBuzzerOff() {
digitalWrite(BUZZER_PIN, LOW);
printf("[BUZZER] OFF - Silence\n");
}
void handleMotion(int motion) {
if (motion == HIGH && motionState == 0) {
motionCount++;
motionState = 1;
printf("\n[PIR] Motion detected!\n");
turnBuzzerOn();
char msg[100];
sprintf(msg, "Motion Detected! Total Count: %d", motionCount);
sendTelegram(msg);
}
else if (motion == LOW && motionState == 1) {
motionState = 0;
printf("\n[PIR] No motion.\n");
turnBuzzerOff();
sendTelegram("Area is Clear");
}
}
void processCommand(const char* msg) {
if (strstr(msg, "/on")) {
systemEnabled = 1;
sendTelegram("System Activated");
printf("[CMD] System turned ON\n");
}
else if (strstr(msg, "/off")) {
systemEnabled = 0;
turnBuzzerOff();
sendTelegram("System Deactivated");
printf("[CMD] System turned OFF\n");
}
else if (strstr(msg, "/status")) {
char info[150];
sprintf(info, "System: %s | Motions: %d",
systemEnabled ? "ON" : "OFF",
motionCount);
sendTelegram(info);
}
else if (strstr(msg, "/reset")) {
motionCount = 0;
sendTelegram("Counter Reset!");
printf("[CMD] Motion counter reset\n");
}
else if (strstr(msg, "/motion_on")) {
pirSensorVal = HIGH;
printf("[SIM] PIR sensor = HIGH (motion simulated)\n");
}
else if (strstr(msg, "/motion_off")) {
pirSensorVal = LOW;
printf("[SIM] PIR sensor = LOW (no motion)\n");
}
else {
printf("[CMD] Unknown command: %s\n", msg);
}
}
void printSeparator() {
printf("\n==========================================\n");
}
void printStatus() {
printSeparator();
printf(" System : %s\n", systemEnabled ? "ON" : "OFF");
printf(" Buzzer : %s\n", buzzerState ? "ON" : "OFF");
printf(" PIR : %s\n", pirSensorVal ? "MOTION" : "CLEAR");
printf(" Count : %d\n", motionCount);
printSeparator();
}
void printMenu() {
printf("\n--- Commands ---\n");
printf(" /on - System ON\n");
printf(" /off - System OFF\n");
printf(" /status - Show status\n");
printf(" /reset - Reset counter\n");
printf(" /motion_on - Simulate motion\n");
printf(" /motion_off - Stop motion\n");
printf(" /quit - Exit\n");
printf("----------------\n");
printf("Enter command: ");
}
void setup() {
printSeparator();
printf(" ESP32 Motion Detection System\n");
printf(" Pure C Simulation\n");
printSeparator();
pinMode(PIR_PIN, 0);
pinMode(BUZZER_PIN, 1);
connectWiFi();
printf("[SYS] Warming up PIR sensor");
fflush(stdout);
int i;
for (i = 0; i < 5; i++) {
printf(".");
fflush(stdout);
}
printf(" Ready!\n");
sendTelegram("System is ONLINE");
}
int main() {
setup();
char input[50];
while (1) {
printMenu();
fgets(input, sizeof(input), stdin);
input[strcspn(input, "\n")] = 0;
if (strstr(input, "/quit")) {
printf("\n[SYS] System shutting down...\n");
sendTelegram("System is OFFLINE");
break;
}
processCommand(input);
if (systemEnabled == 1) {
int motion = readMotion();
handleMotion(motion);
}
}
return 0;
}